/*
 * @Author: your name
 * @Date: 2024-04-17 18:03:07
 * @LastEditTime: 2024-04-17 18:24:59
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\107.二叉树的层序遍历-ii.cpp
 */
/*
 * @lc app=leetcode.cn id=107 lang=cpp
 *
 * [107] 二叉树的层序遍历 II
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> vec;
        if(root == nullptr){
            return vec;
        }   
        queue<TreeNode*> que;
        que.push(root);
        while(!que.empty()){
            int size = que.size();
            vector<int> tmp_vec;
            for(int i = 0; i < size; i++){
                TreeNode* tmp_node = que.front();// 获取队头
                tmp_vec.push_back(tmp_node->val);
                que.pop();
                if(tmp_node->left != nullptr)  que.push(tmp_node->left);
                if(tmp_node->right != nullptr)  que.push(tmp_node->right);

            }
            vec.push_back(tmp_vec);
        }
        reverse(vec.begin(), vec.end());
        return vec;
    
    }
};
// @lc code=end

